package com.park.controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import com.park.model.*;
import com.park.service.*;
import com.util.CCBUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.dao.PosChargeDataDAO;

@Controller
@RequestMapping("smartGate")
public class SmartGateController {
	@Autowired
	HardwareService hardwareService;
	@Autowired
	PosChargeDataService chargeSerivce;
	@Autowired
	PosChargeDataDAO chargeDao;
	@Autowired
	ParkToAliparkService parkToAliparkService;
	@Autowired
	AliParkFeeService aliparkFeeService;
	@Autowired
	MonthUserService monthUserService;
	@Autowired
	AlipayrecordService alipayrecordService;
	@Autowired
	ParkService parkService;
	@Autowired
	FeeCriterionService feeCriterionService;
	@Autowired
	ParkRecordDataService parkRecordDataService;
	@Autowired
	ParkCarAuthorityService parkCarAuthorityService;
	@Autowired
	PosChargeMacService posChargeMacService;
	@Autowired
	SimilarCarNumberService similarCarNumberService;
	@Autowired
	ParkNoticeAuthorityService parkNoticeAuthorityService;
	@Autowired
	FeeOperatorService feeoperatorService;
	@Autowired
	ActiveMqService ActiveMqService;
	@Autowired
	ParkCarAuthority2Service parkcarauthority2Service;
	
	@Autowired
	MonthuserConsumerRecordService monthuserConsumerService;

	@Resource(name = "jedisClient")
	private JedisClient jedisClient;
	
	private int length = 0;

	private static final Log logger = LogFactory.getLog(SmartGateController.class);

	private class ConfigProperties {
		String mac = "";
		String cardNumber = "";
		PosChargeData charge = new PosChargeData();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = null;
		Integer parkId;
		int channelFlag;
		List<Monthuser> monthusers = null;
		Park park = null;
		int monthUserType = 9;
		List<Parkcarauthority> parkcarauthorities = null;// 获取开闸权限
		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		Monthuser monthuserNow = null;
		Boolean isMultiCarsOneCarport = false;
		Boolean isMultiFeeCtriterion = false;
		List<Monthuser> realMonthUsers = null;
		Parknoticeauthority parknoticeauthority = null;
		List<Parktoalipark> parktoaliparks = null;
		Parkcarauthority parkcarauthority = null;
		double shouldChargeMoney = 0;

		protected Boolean getIsMultiFeeCtriterion() {
			return isMultiFeeCtriterion;
		}

		protected void setIsMultiFeeCtriterion(Boolean isMultiFeeCtriterion) {
			this.isMultiFeeCtriterion = isMultiFeeCtriterion;
		}

		protected double getShouldChargeMoney() {
			return shouldChargeMoney;
		}

		protected void setShouldChargeMoney(double shouldChargeMoney) {
			this.shouldChargeMoney = shouldChargeMoney;
		}

		protected Parkcarauthority getParkcarauthority() {
			return parkcarauthority;
		}

		protected void setParkcarauthority(Parkcarauthority parkcarauthority) {
			this.parkcarauthority = parkcarauthority;
		}

		protected int getMonthUserType() {
			return monthUserType;
		}

		protected void setMonthUserType(int monthUserType) {
			this.monthUserType = monthUserType;
		}

		protected List<Parktoalipark> getParktoaliparks() {
			return parktoaliparks;
		}

		protected void setParktoaliparks(List<Parktoalipark> parktoaliparks) {
			this.parktoaliparks = parktoaliparks;
		}

		protected String getMac() {
			return mac;
		}

		protected void setMac(String mac) {
			this.mac = mac;
		}

		protected String getCardNumber() {
			return cardNumber;
		}

		protected void setCardNumber(String cardNumber) {
			this.cardNumber = cardNumber;
		}

		protected PosChargeData getCharge() {
			return charge;
		}

		protected void setCharge(PosChargeData charge) {
			this.charge = charge;
		}

		protected Map<String, Object> getDataMap() {
			return dataMap;
		}

		protected void setDataMap(Map<String, Object> dataMap) {
			this.dataMap = dataMap;
		}

		protected List<Map<String, Object>> getInfos() {
			return infos;
		}

		protected void setInfos(List<Map<String, Object>> infos) {
			this.infos = infos;
		}

		protected Integer getParkId() {
			return parkId;
		}

		protected void setParkId(Integer parkId) {
			this.parkId = parkId;
		}

		protected int getChannelFlag() {
			return channelFlag;
		}

		protected void setChannelFlag(int channelFlag) {
			this.channelFlag = channelFlag;
		}

		protected List<Monthuser> getMonthusers() {
			return monthusers;
		}

		protected void setMonthusers(List<Monthuser> monthusers) {
			this.monthusers = monthusers;
		}

		protected Park getPark() {
			return park;
		}

		protected void setPark(Park park) {
			this.park = park;
		}

		protected List<Parkcarauthority> getParkcarauthorities() {
			return parkcarauthorities;
		}

		protected void setParkcarauthorities(List<Parkcarauthority> parkcarauthorities) {
			this.parkcarauthorities = parkcarauthorities;
		}

		protected boolean isMonthUser() {
			return isMonthUser;
		}

		protected void setisMonthUser(boolean isMonthUser) {
			this.isMonthUser = isMonthUser;
		}

		protected boolean isRealMonthUser() {
			return isRealMonthUser;
		}

		protected void setisRealMonthUser(boolean isRealMonthUser) {
			this.isRealMonthUser = isRealMonthUser;
		}

		protected Monthuser getMonthuserNow() {
			return monthuserNow;
		}

		protected void setMonthuserNow(Monthuser monthuserNow) {
			this.monthuserNow = monthuserNow;
		}

		protected Boolean getIsMultiCarsOneCarport() {
			return isMultiCarsOneCarport;
		}

		protected void setIsMultiCarsOneCarport(Boolean isMultiCarsOneCarport) {
			this.isMultiCarsOneCarport = isMultiCarsOneCarport;
		}

		protected List<Monthuser> getRealMonthUsers() {
			return realMonthUsers;
		}

		protected void setRealMonthUsers(List<Monthuser> realMonthUsers) {
			this.realMonthUsers = realMonthUsers;
		}

		protected Parknoticeauthority getParknoticeauthority() {
			return parknoticeauthority;
		}

		protected void setParknoticeauthority(Parknoticeauthority parknoticeauthority) {
			this.parknoticeauthority = parknoticeauthority;
		}
		
		

		@Override
		public String toString() {
			return "ConfigProperties [mac=" + mac + ", cardNumber=" + cardNumber + ", charge=" + charge + ", dataMap="
					+ dataMap + ", infos=" + infos + ", parkId=" + parkId + ", channelFlag=" + channelFlag
					+ ", monthusers=" + monthusers + ", park=" + park + ", monthUserType=" + monthUserType
					+ ", parkcarauthorities=" + parkcarauthorities + ", isMonthUser=" + isMonthUser
					+ ", isRealMonthUser=" + isRealMonthUser + ", monthuserNow=" + monthuserNow
					+ ", isMultiCarsOneCarport=" + isMultiCarsOneCarport + ", isMultiFeeCtriterion="
					+ isMultiFeeCtriterion + ", realMonthUsers=" + realMonthUsers + ", parknoticeauthority="
					+ parknoticeauthority + ", parktoaliparks=" + parktoaliparks + ", parkcarauthority="
					+ parkcarauthority + ", shouldChargeMoney=" + shouldChargeMoney + "]";
		}

		protected boolean isEnter() {
			if (this.channelFlag == 1) {
				return true;
			} else {
				return false;
			}
		}

		protected boolean isExit() {
			if (this.channelFlag == 0) {
				return true;
			} else {
				return false;
			}
		}
	}

	private String getSimilarPlateNumber(String plateNumber, Integer parkId) {
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(plateNumber,
				parkId);
		String realPlateNumber = plateNumber;
		if (!similarcarnumbers.isEmpty()) {
			realPlateNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("发现相似车牌，更正车牌为:" + plateNumber);
		}
		return realPlateNumber;
	}

	private String generalFormatTime(Date timePattern) {
		return new SimpleDateFormat(Constants.DATEFORMAT).format(timePattern);
	}

	private boolean checkMultiCarport(Park park) {
		boolean result = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {

			result = true;
		}
		return result;
	}

	private boolean checkMultiFeeCtriterion(Park park) {
		boolean result = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			result = true;
		}
		return result;
	}

	private List<Monthuser> getMonthUserCars(Monthuser monthuserNow) {
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null && !monthuserNow.getCardnumber().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}
		return realMonthUsers;
	}

	private void setLargeCarStatus(boolean largeCar, PosChargeData charge) {
		if (largeCar == true) {
			charge.setIsLargeCar(true);
		}

	}

	private void handlePostMessagetToGuiyang(Integer parkId, String cardNumber) {
		if (parkId == 334) {
			Map<String, String> guiyang = new HashMap<>();
			guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
			guiyang.put("type", "in");
			guiyang.put("plateNum", cardNumber);
			guiyang.put("enterTime", generalFormatTime(new Date()));
			ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
		}
	}

	private void hanlderPoschargeInfoExit(ConfigProperties configProperties, PosChargeData payRet) {
		Park park = configProperties.getPark();
		Map<String, Object> dataMap = configProperties.getDataMap();
		List<Monthuser> monthusers = configProperties.getMonthusers();
		PosChargeData charge = configProperties.getCharge();
		Parkcarauthority parkcarauthority = configProperties.getParkcarauthority();
		switch (configProperties.getMonthUserType()) {
		case 0:
			if (parkcarauthority.getMonth() != true) {
				dataMap.put("aT", "0");
			}
			break;
		case 1:
			charge.setParkDesc(park.getName() + "-预约");
			if (parkcarauthority.getYuyue() != true) {
				dataMap.put("aT", "0");
			}
			if (payRet.getChargeMoney() <= 0) {
				dataMap.put("aT", "1");
			}
			break;
		case 2:
			charge.setParkDesc(park.getName() + "-储值卡");
			if (parkcarauthority.getTypea() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 3:
			charge.setParkDesc(park.getName() + "-包月A2");
			if (parkcarauthority.getTypeb() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 4:
			charge.setParkDesc(park.getName() + "-包月B");
			if (parkcarauthority.getTypec() != true) {
				dataMap.put("aT", "0");
			}
			break;
		case 5:
			charge.setParkDesc(park.getName() + "-包月D");
			if (parkcarauthority.getTyped() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 6:
			charge.setParkDesc(park.getName() + "-包月E");
			if (parkcarauthority.getTypee() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 8:
			charge.setParkDesc(park.getName() + "-包月过期");
			if (parkcarauthority.getMonthexpired() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 9:
			if (parkcarauthority.getTemporary() != true && configProperties.getShouldChargeMoney() > 0) {
				dataMap.put("aT", "0");
			}
			if (parkcarauthority.getTemporary0() != true && configProperties.getShouldChargeMoney() < 0.1) {
				dataMap.put("aT", "0");
			}
			break;
		default:
			break;
		}

	}

	private void hanlderPoschargeInfoEnter(ConfigProperties configProperties) {
		Park park = configProperties.getPark();
		Map<String, Object> dataMap = configProperties.getDataMap();
		List<Monthuser> monthusers = configProperties.getMonthusers();
		PosChargeData charge = configProperties.getCharge();
		Parkcarauthority parkcarauthority = configProperties.getParkcarauthority();
		switch (configProperties.getMonthUserType()) {
		case 0:
			charge.setParkDesc(park.getName() + "-包月车");
			charge.setRejectReason(monthusers.get(0).getOwner());
			if (parkcarauthority.getMonth() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 1:
			charge.setParkDesc(park.getName() + "-预约");
			if (parkcarauthority.getYuyue() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 2:
			charge.setParkDesc(park.getName() + "-储值卡");
			if (parkcarauthority.getTypea() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 3:
			charge.setParkDesc(park.getName() + "-包月A2");
			if (parkcarauthority.getTypeb() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 4:
			charge.setParkDesc(park.getName() + "-包月B");
			if (parkcarauthority.getTypec() != true) {
				dataMap.put("aT", "0");
			}
			break;
		case 5:
			charge.setParkDesc(park.getName() + "-包月D");
			if (parkcarauthority.getTyped() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 6:
			charge.setParkDesc(park.getName() + "-包月E");
			if (parkcarauthority.getTypee() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 8:
			charge.setParkDesc(park.getName() + "-包月过期");
			if (parkcarauthority.getMonthexpired() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 9:
			charge.setParkDesc(park.getName() + "-临停车");
			if (parkcarauthority.getTemporary() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 10:
			charge.setParkDesc(park.getName() + "-VIP");
			if (parkcarauthority.getTemporary() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 11:
			charge.setParkDesc(park.getName() + "-VIP");
			if (parkcarauthority.getTemporary() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 12:
			charge.setParkDesc(park.getName() + "-VIP");
			if (parkcarauthority.getTemporary() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 13:
			charge.setParkDesc(park.getName() + "-VIP");
			if (parkcarauthority.getTemporary() != true) {
				dataMap.put("aT", "0");

			}
			break;
		case 14:
			charge.setParkDesc(park.getName() + "-VIP");
			if (parkcarauthority.getTemporary() != true) {
				dataMap.put("aT", "0");

			}
			break;
		default:
			break;
		}
		if (!monthusers.isEmpty()) {
			charge.setRejectReason(monthusers.get(0).getOwner());
		}

	}

	private void setPosChargeInfo(ConfigProperties configProperties) {
		Park park = configProperties.getPark();
		Integer parkId = configProperties.getParkId();
		String mac = configProperties.getMac();
		List<Monthuser> monthusers = configProperties.getMonthusers();
		String cardNumber = configProperties.getCardNumber();
		Map<String, Object> dataMap = configProperties.getDataMap();
		PosChargeData charge = configProperties.getCharge();
		if (!monthusers.isEmpty()) {
			charge.setRejectReason(monthusers.get(0).getOwner());
		}

		dataMap.put("cT", "in");
		charge.setCardNumber(cardNumber);
		charge.setParkId(parkId);
		charge.setPosId(mac);
		// charge.setOperatorId(mac);
		try {
			charge.setEntranceDate(generalFormatTime(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void handleMultiCarIn(ConfigProperties configProperties) {
		String cardNumber = configProperties.getCardNumber();
		Park park = configProperties.getPark();
		Boolean isMultiCarsOneCarport = configProperties.getIsMultiCarsOneCarport();
		Boolean isRealMonthUser = configProperties.isRealMonthUser;
		List<Monthuser> realMonthUsers = configProperties.getRealMonthUsers();
		Map<String, Object> dataMap = configProperties.getDataMap();
		Monthuser monthuserNow = configProperties.getMonthuserNow();
		PosChargeData charge = configProperties.getCharge();
		Boolean isMonthUserCarIn = false;
		if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
			for (Monthuser tmMonthuser : realMonthUsers) {
				if (tmMonthuser.getPlatecolor() != null & tmMonthuser.getPlatecolor().equals("多车包月入场")) {
					if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
						isMonthUserCarIn = true;
					}
				}
			}
			monthuserNow.setPlatecolor("多车包月入场");
			if (isMonthUserCarIn) {
				charge.setParkDesc(park.getName() + "-包月转临停");
				monthuserNow.setPlatecolor("包月转为临停");
				dataMap.put("uT", "0");
				try {
					dataMap.remove("ds");
					dataMap.remove("eD");
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
			monthUserService.updateByPrimaryKeySelective(monthuserNow);
		}
	}

	private void handleAutoExit(ConfigProperties configProperties) {// 处理平台清场事宜
		String cardNumber = configProperties.getCardNumber();
		Integer parkId = configProperties.getParkId();
		Park park = configProperties.getPark();
		PosChargeData charge = configProperties.getCharge();
		String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
		if (redisId != null) {
			PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
			logger.info("posChargeDatatmp:"+posChargeDatatmp);
			if (!posChargeDatatmp.isPaidCompleted()) {
				posChargeDatatmp.setChargeMoney(0);
				posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
				posChargeDatatmp.setPaidCompleted(true);
				posChargeDatatmp.setOperatorId("redis");
				posChargeDatatmp.setRejectReason("平台清场");
				logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
				chargeSerivce.update(posChargeDatatmp);
			}

			jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
			jedisClient.del("query" + charge.getCardNumber());
			logger.info("重新入场 " + cardNumber + " id" + charge.getId());

		}

		jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
		logger.info(cardNumber + "设置redis值" + charge.getId());
		jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
		jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
		jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()), Constants.carMaxPeriod);
	}

	private void handlePostToAlipay(ConfigProperties configProperties) {
		String cardNumber = configProperties.getCardNumber();
		List<Parktoalipark> parktoaliparks = configProperties.getParktoaliparks();
		Parknoticeauthority parknoticeauthority = configProperties.getParknoticeauthority();
		try {

			// 此处的处理逻辑是，本停车场在支付宝停车场注册过，同时开头了入场通知推送才可以
			if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
				Parktoalipark parktoalipark = parktoaliparks.get(0);
				Map<String, String> argstoali = new HashMap<>();
				argstoali.put("parking_id", parktoalipark.getAliparkingid());
				argstoali.put("car_number", cardNumber);
				argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

			} else {
				logger.info(cardNumber + "handlePostToAlipay-推送失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(cardNumber + "支付宝推送异常!");
		}
	}

	private boolean checkInfoValidation(List<Map<String, Object>> infos) {

		boolean result = true;
		if (infos.isEmpty()) {
			result = false;
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			result = false;
		}
		return result;
	}

	private void handlePushtoMq(ConfigProperties configProperties) {
		Park park = configProperties.getPark();
		PosChargeData charge = configProperties.getCharge();
		Integer parkId = configProperties.getParkId();
		if (park.getDescription().contains("本地计费")) {
			charge.setEntrance(true);
			ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), configProperties.getMac(),
					park.getPortLeftCount(), configProperties.getMonthUserType());
		}
	}

	private void handlePushtoMessageQueue(ConfigProperties configProperties) {
		handlePushtoMq(configProperties);
		handlePostToAlipay(configProperties);
	}

	private boolean addCount(int count) {
		int maxCount = count/1000;
		int crrentCount = count%1000;
		if(maxCount == length) {
			return true;
		}else {
		if(crrentCount + 1 > maxCount) {
			return false;
		}else {
			return true;
		}
		}
	}
	private boolean deleteCount(int count) {
		int maxCount = count/1000;
		int crrentCount = count%1000;
		if(maxCount == length) {
			return true;
		}else {
		if(crrentCount - 1 < 0) {
			return false;
		}else {
			return true;
		}
		}
	}
	private String handleMonthUserCar(ConfigProperties configProperties) {
		String endTime = null;
		if (!configProperties.getMonthusers().isEmpty()) {
			configProperties.setisMonthUser(true);
			for (Monthuser tmpMonthuser : configProperties.getMonthusers()) {
				logger.info(configProperties.getCardNumber() + "月卡id:" + tmpMonthuser.getId() + "类型:"
						+ tmpMonthuser.getType() + "到期时间:" + Utility.formatDatetime(tmpMonthuser.getEndtime()));
				endTime = Utility.formatDatetime(tmpMonthuser.getEndtime());
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				configProperties.setMonthuserNow(tmpMonthuser);
				if (tmpMonthuser.getType() == 0) {// 月卡
					configProperties.setMonthUserType(0);
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						configProperties.getDataMap().put("ds", String.valueOf(leftDays));
						configProperties.setisRealMonthUser(true);
						configProperties.getDataMap().put("my","0.0");
						configProperties.getDataMap().put("eD", generalFormatTime(tmpMonthuser.getEndtime()));
						break;
					} else {
						configProperties.getDataMap().put("ds", "-1");
						configProperties.getDataMap().put("uT", "1");
						configProperties.setMonthUserType(8);// 月卡过期
					}
				} else {
					if (diff >= 0) {
						configProperties.setMonthUserType(tmpMonthuser.getType());
						break;
					} else {
						configProperties.setisMonthUser(false);

					}
				}
			}

		}
		return endTime;
	}
	
	private String handleMonthUserCarV2(ConfigProperties configProperties,int direction) {
		logger.info("ismonthuser");
		logger.info(configProperties);
		String endTime = null;
		if(direction == 1) {
			logger.info("direction == 1");
		if (!configProperties.getMonthusers().isEmpty()) {
			int count = configProperties.getMonthusers().get(0).getStatus();
			length = configProperties.getMonthusers().get(0).getPlatenumber().split(",").length+1;
			if(deleteCount(count)) {
				logger.info("deleteCount(count)");
				configProperties.setisMonthUser(true);
				for (Monthuser tmpMonthuser : configProperties.getMonthusers()) {
					logger.info(configProperties.getCardNumber() + "月卡id:" + tmpMonthuser.getId() + "类型:"
							+ tmpMonthuser.getType() + "到期时间:" + Utility.formatDatetime(tmpMonthuser.getEndtime()));
					if (configProperties.getPark().getDescription().contains("月卡7天延期")) {
						tmpMonthuser.setEndtime1(new Date(tmpMonthuser.getEndtime().getTime() + 7 * 24 * 60 * 60 * 1000));
					}
					endTime = Utility.formatDatetime(tmpMonthuser.getEndtime());
					Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
					configProperties.setMonthuserNow(tmpMonthuser);
					boolean isVip=tmpMonthuser.getType()>=10&&tmpMonthuser.getType()<=14;
					if (tmpMonthuser.getType() == 0 || isVip) {// 月卡
						if(tmpMonthuser.getType() == 0) {
							configProperties.setMonthUserType(0);
						}else if(isVip) {
							configProperties.setMonthUserType(tmpMonthuser.getType());
						}
						
						if (diff > 0) {
							int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
							configProperties.getDataMap().put("ds", String.valueOf(leftDays));
							configProperties.setisRealMonthUser(true);
							configProperties.getDataMap().put("my","0.0");
							configProperties.getDataMap().put("eD", generalFormatTime(tmpMonthuser.getEndtime()));
							break;
						} else {
							configProperties.getDataMap().put("ds", "-1");
							configProperties.getDataMap().put("uT", "0");
							configProperties.setMonthUserType(8);// 月卡过期
						}
					} else {
						if (diff >= 0) {
							configProperties.setMonthUserType(tmpMonthuser.getType());
							break;
						} else {
							configProperties.setisMonthUser(false);

						}
					}
				}
				configProperties.getMonthusers().get(0).setStatus(count-1);
				monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
			}else {
				configProperties.setisMonthUser(false);
			}
		}
		else {
			configProperties.setisMonthUser(false);
		}
		}else {
			logger.info("direction == 2");
			if (!configProperties.getMonthusers().isEmpty()) {
				int count = configProperties.getMonthusers().get(0).getStatus();
				length = configProperties.getMonthusers().get(0).getPlatenumber().split(",").length+1;
				if(configProperties.getMonthusers().get(0).getType() == 17){
					configProperties.getDataMap().put("aT", "0");
					configProperties.getDataMap().put("uT", configProperties.getMonthusers().get(0).getType());
					configProperties.getDataMap().put("cT", "out");
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				for (Monthuser tmpMonthuser : configProperties.getMonthusers()) {
					Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
					logger.info(configProperties.getCardNumber() + "月卡id:" + tmpMonthuser.getId() + "类型:"
							+ tmpMonthuser.getType() + "到期时间:" + Utility.formatDatetime(tmpMonthuser.getEndtime()));
					if (configProperties.getPark().getDescription().contains("月卡7天延期")) {
						tmpMonthuser.setEndtime1(new Date(tmpMonthuser.getEndtime().getTime() + 7 * 24 * 60 * 60 * 1000));
					}
					endTime = Utility.formatDatetime(tmpMonthuser.getEndtime());
					configProperties.setMonthuserNow(tmpMonthuser);
					if (diff < 0) {
						configProperties.getDataMap().put("ds", "-1");
						configProperties.getDataMap().put("uT", "0");
						configProperties.setMonthUserType(8);// 月卡过期
					}else {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						configProperties.setisRealMonthUser(true);
						configProperties.getDataMap().put("ds", String.valueOf(leftDays));
						configProperties.getDataMap().put("eD", generalFormatTime(tmpMonthuser.getEndtime()));
						configProperties.setisMonthUser(true);
						configProperties.setMonthUserType(tmpMonthuser.getType());
					}
				}
				if(addCount(count)) {
					configProperties.getMonthusers().get(0).setStatus(count+1);
					monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
				}
		}
		}
		return endTime;
	}

	private ConfigProperties handleMonthUserPriority(ConfigProperties configProperties) {
		System.out.println("handleMonthUserPriority"+configProperties);
		if (!configProperties.isMonthUser) {
			configProperties.getDataMap().put("uT", "0");
		} else {
			configProperties.getDataMap().put("uT", "1");
			// 判断是否是预约车
			if (configProperties.getMonthUserType() == 1) {
				configProperties.getDataMap().put("uT", "2");
				configProperties.getDataMap().put("my", "0.0");
			}else if(configProperties.getMonthUserType() == 2){
				configProperties.getDataMap().put("uT", "3");
			}else if(configProperties.getMonthUserType() >=10 && configProperties.getMonthUserType()<=14){
				configProperties.getDataMap().put("uT", configProperties.getMonthUserType());
			}else if(configProperties.getMonthUserType() == 8){
				configProperties.getDataMap().put("ds", "-1");
				configProperties.getDataMap().put("uT", "0");
			}
		}
		return configProperties;
	}

	private void getParkEnterAuthority(ConfigProperties configProperties) {
		configProperties.setParkcarauthority(configProperties.getParkcarauthorities().get(0));
		for (Parkcarauthority tmParkcarauthority : configProperties.getParkcarauthorities()) {
			if (tmParkcarauthority.getChannel() == 1) {
				configProperties.setParkcarauthority(tmParkcarauthority);
				break;
			}
		}
	}

	private void getParkExitAuthority(ConfigProperties configProperties) {
		configProperties.setParkcarauthority(configProperties.getParkcarauthorities().get(0));
		for (Parkcarauthority tmParkcarauthority : configProperties.getParkcarauthorities()) {
			if (tmParkcarauthority.getChannel() == 0) {
				configProperties.setParkcarauthority(tmParkcarauthority);
				break;
			}
		}
	}
	boolean isThirdPartyPay(int payType) {
		boolean res = false;
		if(payType != 2 && payType != 9 && payType != 20) {
			res = true;
		}
		return res;
	}

	private Date getPaidDate(Date date, PosChargeData posChargeData) {
		Date res = date;
		if (isThirdPartyPay(posChargeData.getPayType() )) {
			logger.info(posChargeData.getCardNumber() + "已经通过移动进行支付");
			List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
			if (alipayrecords.isEmpty()) {
				res = new Date();
			} else {
				res = alipayrecords.get(0).getDate();
				logger.info(posChargeData.getCardNumber() + "已查询到支付时间!" + generalFormatTime(res));
			}
		}
		return res;
	}

	private void handlePushToWechatExitNotice(ConfigProperties configProperties, PosChargeData posChargeData) {
		if (configProperties.getParknoticeauthority() != null
				&& configProperties.getParknoticeauthority().getWeixin() == true) {
			Map<String, String> argstoali = new HashMap<>();
			argstoali.put("parkName", posChargeData.getParkDesc());
			argstoali.put("carNumber", posChargeData.getCardNumber());
			argstoali.put("enterTime", generalFormatTime(posChargeData.getEntranceDate()));
			argstoali.put("exitTime",
					generalFormatTime(posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
			ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
		}
	}

	private String getParkingTimeLen(long exitTime, long enterTime) {
		long exitDate = (exitTime - enterTime) / (1000 * 60);
		if(exitDate > 0) {
			return String.valueOf(exitDate);
		}else {
			return "13";
		}
	}

	// 王小强修改touched接口
	@RequestMapping(value = "hefeitouchedV2", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String wxqtouched(@RequestBody Map<String, String> args) throws Exception {
		logger.info("hefeitouchedV2入口:"+args);
		String expirationTime = null;
		ConfigProperties configProperties = new ConfigProperties();
		configProperties.setMac(args.get("mac"));
		configProperties.setCardNumber(args.get("cardNumber"));
		logger.info(configProperties.getCardNumber() + "--->进入[hefeitouchedV2]车辆入场/出场接口");
		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		configProperties.setInfos(hardwareService.getInfoByMac(configProperties.getMac()));
		if (!checkInfoValidation(configProperties.getInfos())) {
			return Utility.createJsonMsgWithoutMsg(1002, "get mac info failed!");
		}
		Map<String, Object> info = configProperties.getInfos().get(0);
		configProperties.setChannelFlag((int) info.get("channelFlag"));
		configProperties.setParkId((Integer) info.get("parkID"));
		configProperties.setMonthusers(
				monthUserService.getByCarnumberAndPark(configProperties.getCardNumber(), configProperties.getParkId()));
		configProperties.setPark(parkService.getParkById(configProperties.getParkId()));
		configProperties.setParkcarauthorities(parkCarAuthorityService.getByParkId(configProperties.getParkId()));
		configProperties
				.setCardNumber(getSimilarPlateNumber(configProperties.getCardNumber(), configProperties.getParkId()));
		if (configProperties.getParkcarauthorities().isEmpty()) {
			return null;
		}

		configProperties.getDataMap().put("aT", "1");
		configProperties.setParktoaliparks(parkToAliparkService.getByParkId(configProperties.getParkId()));
		if (checkMultiCarport(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入一位多车停车场");
			configProperties.setIsMultiCarsOneCarport(true);
		}
		if (checkMultiFeeCtriterion(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入多计费标准停车场");
			configProperties.setIsMultiFeeCtriterion(true);
		}
		
		
		expirationTime = handleMonthUserCar(configProperties);

		// 判断是否有多个车
		configProperties
				.setParknoticeauthority(parkNoticeAuthorityService.getByParkId(configProperties.getParkId()).get(0));
		configProperties.setRealMonthUsers(getMonthUserCars(configProperties.getMonthuserNow()));

		handleMonthUserPriority(configProperties);
		// 入口硬件
		if (configProperties.isEnter()) {
			
			setLargeCarStatus(largeCar, configProperties.getCharge());
			configProperties.getCharge().setParkDesc(configProperties.getPark().getName() + "-临停车");

			// 取得入口权限
			getParkEnterAuthority(configProperties);
			hanlderPoschargeInfoEnter(configProperties);
			// 处理一车多位入场
			setPosChargeInfo(configProperties);
			// redis查询
			try {
				String redisStr = jedisClient
						.get("carIn" + configProperties.getParkId() + configProperties.getCardNumber());
				if (redisStr != null) {
					logger.info("redis检测到重复入场" + configProperties.getCardNumber() + redisStr);
				} else {
					int num = chargeSerivce.insert(configProperties.getCharge());
					// 清场
					handleAutoExit(configProperties);
					if (num == 1) {
						handlePushtoMessageQueue(configProperties);
						logger.info(
								configProperties.getCardNumber() + "入场成功!" + configProperties.getDataMap().toString());
					}
				}
			} catch (Exception e) {
				logger.info(configProperties.getCardNumber() + "redis异常" + e);
			}

			logger.info(configProperties.getCardNumber() + "<---离开[hefeitouched]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
		} else { // 处理出场逻辑
			configProperties.getDataMap().put("cT", "out");
			configProperties.getDataMap().put("pT", "9");
			List<PosChargeData> queryCharges = null;
			logger.info(configProperties.getCardNumber() + "开始出场");
			if (configProperties.getMonthuserNow() != null) {
				configProperties.getRealMonthUsers().add(configProperties.getMonthuserNow());
			}
			// 取得出口权限
			getParkExitAuthority(configProperties);
			try {
				logger.info(configProperties.getCardNumber() + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
						configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
						configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
						configProperties.getMonthUserType(), false,expirationTime);
				if (CollectionUtils.isEmpty(queryCharges)) {
					PosChargeData posChargeData = outParkingFuzzyMatching(configProperties.getParkId(),
							configProperties.getCardNumber());// 车辆出场车牌模糊匹配
					if (posChargeData != null) {
						queryCharges = chargeSerivce.getDebtWithDataV2(posChargeData.getCardNumber(),
								configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
								configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
								configProperties.getMonthUserType(), false,expirationTime);
					}

				}

			} catch (Exception e) {
				return Utility.createJsonMsgWithoutMsg(1002, e);
			}
			logger.info(configProperties.getCardNumber() + "计费结束----<");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info(configProperties.getCardNumber() + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(configProperties.getPark().getFeeCriterionId());

				if (configProperties.isRealMonthUser && configProperties.getRealMonthUsers().size() == 1) {
					logger.info(configProperties.getCardNumber() + "<---离开[hefeitouched]车辆入场/出场接口包月用户离场，返回"
							+ Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap()));
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(configProperties.getCardNumber() + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = chargeSerivce
						.getLastRecordWithPark(configProperties.getCardNumber(), 1, configProperties.getParkId());
				if (posChargeDataList.isEmpty()) {
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", "13");
					logger.info(configProperties.getCardNumber() + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				
				configProperties.getDataMap().put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				handlePushToWechatExitNotice(configProperties, posChargeData);
				Date payDate = posChargeData.getExitDate();

				payDate = getPaidDate(payDate, posChargeData);// 如果进行微信支付宝支付，则需要获取支付时间
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = (new Date().getTime() - payDate.getTime()) / (1000 * 60);

				// 防止逃费
				logger.info(configProperties.getCardNumber() + "开始执行防逃费功能---->");
				if (posChargeData.getPayType() == 9 && diff < 60) {// 1小时内防逃费??到底防逃费防止多久？
					logger.info(configProperties.getCardNumber() + "防逃费，而且时间小于12小时");
					configProperties.getDataMap().put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						configProperties.getDataMap().put("aT", "0");
					} else {
						configProperties.getDataMap().put("aT", "1");
					}
					configProperties.getDataMap().put("eD", getParkingTimeLen(posChargeData.getExitDate().getTime(),
							posChargeData.getEntranceDate().getTime()));
					logger.info(configProperties.getCardNumber() + "结束执行防逃费功能----<防逃费:"
							+ configProperties.getDataMap().toString());
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				if (diff < 15) {
					logger.info(configProperties.getCardNumber() + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", String.valueOf(diff));// 支付时间到现在为止小于15分钟时，直接返回时间即可
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 超过了15分钟
//				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
//						&& posChargeData.getPayType() < 20) {
					
					else if (isThirdPartyPay(posChargeData.getPayType())){
					logger.info(configProperties.getCardNumber() + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(configProperties.getPark().getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(configProperties.getCardNumber() + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + configProperties.getParkId() + "-" + charge2.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);

					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "前车位数"
							+ configProperties.getPark().getPortLeftCount());
					parkService.updateLeftPortCount(configProperties.getParkId(),
							configProperties.getPark().getPortLeftCount() + 1);
					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "后车位数"
							+ configProperties.getPark().getPortLeftCount() + 1);

					jedisClient.del("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber());
					jedisClient.del("query" + configProperties.getCardNumber());
					logger.info("支付完超15分钟重新入场 " + configProperties.getCardNumber() + " id" + charge2.getId());
					jedisClient.set("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, configProperties.getDataMap());
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
							configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
							configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
							configProperties.getMonthUserType(), false,expirationTime);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(configProperties.getCardNumber() + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(configProperties.getMac());
					if (posChargeData.getParkId() == configProperties.getParkId().intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				configProperties.setShouldChargeMoney(payRet.getChargeMoney());
				
				num = chargeSerivce.update(payRet);
			} else {

				configProperties.getDataMap().put("eD", "14");
				configProperties.getDataMap().put("my", "0.0");
				logger.info(configProperties.getCardNumber() + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}

			logger.info(configProperties.getCardNumber() + "权限-" + configProperties.getMonthUserType());
			hanlderPoschargeInfoExit(configProperties, payRet);

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())
					|| configProperties.getPark().getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, configProperties.getParkId().intValue())) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (configProperties.getParknoticeauthority() != null
					&& configProperties.getParknoticeauthority().getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime", generalFormatTime(payRet.getEntranceDate()));
					argstoali.put("exitTime", generalFormatTime(payRet.getExitDate()));
					logger.info(configProperties.getCardNumber() + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(configProperties.getCardNumber() + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsgWithoutMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			if(diff2 < 0) {
				configProperties.getDataMap().put("eD","13");
			}else {
			configProperties.getDataMap().put("eD",
					String.valueOf((diff2 / (60 * 1000)) > 6360 ? 13 : (diff2 / (60 * 1000))));
			}
			if (!configProperties.isRealMonthUser) {
				configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
			}

			if (configProperties.isMultiCarsOneCarport && configProperties.isRealMonthUser
					&& configProperties.getRealMonthUsers().size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
					configProperties.getDataMap().put("uT", "0");
				}
			}
			configProperties.getDataMap().put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				posChargeMacService.insertSelective(poschargemac);
				logger.info(configProperties.getCardNumber() + "出场成功!" + configProperties.getDataMap().toString());

				parkService.updateLeftPortCount(configProperties.getParkId(),
						configProperties.getPark().getPortLeftCount() + 1);
				logger.info(configProperties.getCardNumber() + "<---离开[hefeitouched]车辆入场/出场接口");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			} else {
				return Utility.createJsonMsgWithoutMsg(1001, "ok");
			}
		}

	}
	@RequestMapping(value = "hefeitouchedV3", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String hefeitouchedV3(@RequestBody Map<String, String> args) throws Exception {
		logger.info("[hefeitouchedV3]入口:"+args);
		String expirationTime = null;
		ConfigProperties configProperties = new ConfigProperties();
		configProperties.setMac(args.get("mac"));
		configProperties.setCardNumber(args.get("cardNumber"));
		logger.info(configProperties.getCardNumber() + "--->进入[hefeitouchedV3]车辆入场/出场接口");
		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		configProperties.setInfos(hardwareService.getInfoByMac(configProperties.getMac()));
		if (!checkInfoValidation(configProperties.getInfos())) {
			return Utility.createJsonMsgWithoutMsg(1002, "get mac info failed!");
		}
		Map<String, Object> info = configProperties.getInfos().get(0);
		configProperties.setChannelFlag((int) info.get("channelFlag"));
		configProperties.setParkId((Integer) info.get("parkID"));
		configProperties.setMonthusers(
				monthUserService.getByCarnumberAndPark(configProperties.getCardNumber(), configProperties.getParkId()));
		configProperties.setPark(parkService.getParkById(configProperties.getParkId()));
		configProperties.setParkcarauthorities(parkCarAuthorityService.getByParkId(configProperties.getParkId()));
		configProperties
				.setCardNumber(getSimilarPlateNumber(configProperties.getCardNumber(), configProperties.getParkId()));
		if (configProperties.getParkcarauthorities().isEmpty()) {
			return null;
		}
		logger.info("第一次设置aT");
		configProperties.getDataMap().put("aT", "1");
		configProperties.setParktoaliparks(parkToAliparkService.getByParkId(configProperties.getParkId()));
		if (checkMultiCarport(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入一位多车停车场");
			configProperties.setIsMultiCarsOneCarport(true);
		}
		if (checkMultiFeeCtriterion(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入多计费标准停车场");
			configProperties.setIsMultiFeeCtriterion(true);
		}
		expirationTime = handleMonthUserCar(configProperties);

		// 判断是否有多个车
		configProperties
				.setParknoticeauthority(parkNoticeAuthorityService.getByParkId(configProperties.getParkId()).get(0));
		configProperties.setRealMonthUsers(getMonthUserCars(configProperties.getMonthuserNow()));

		handleMonthUserPriority(configProperties);
		// 入口硬件
		if (configProperties.isEnter()) {
			setLargeCarStatus(largeCar, configProperties.getCharge());
			configProperties.getCharge().setParkDesc(configProperties.getPark().getName() + "-临停车");

			// 取得入口权限
			getParkEnterAuthority(configProperties);
			hanlderPoschargeInfoEnter(configProperties);
			// 处理一车多位入场
			setPosChargeInfo(configProperties);
			// redis查询
//			try {
				String redisStr = jedisClient
						.get("carIn" + configProperties.getParkId() + configProperties.getCardNumber());
				if (redisStr != null) {
					logger.info("redis检测到重复入场" + configProperties.getCardNumber() + redisStr);
				} else {
					int num = chargeSerivce.insert(configProperties.getCharge());
					// 清场
					handleAutoExit(configProperties);
					if (num == 1) {
						handlePushtoMessageQueue(configProperties);
						logger.info(
								configProperties.getCardNumber() + "入场成功!" + configProperties.getDataMap().toString());
					}
				}
//			} catch (Exception e) {
//				logger.info(configProperties.getCardNumber() + "redis异常" + e);
//			}

			logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV3]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
		} else { // 处理出场逻辑
			configProperties.getDataMap().put("cT", "out");
			configProperties.getDataMap().put("pT", "9");
			List<PosChargeData> queryCharges = null;
			logger.info(configProperties.getCardNumber() + "开始出场");
			if (configProperties.getMonthuserNow() != null) {
				configProperties.getRealMonthUsers().add(configProperties.getMonthuserNow());
			}
			// 取得出口权限
			getParkExitAuthority(configProperties);
			try {
				logger.info(configProperties.getCardNumber() + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
						configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
						configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
						configProperties.getMonthUserType(), false,expirationTime);
				if (CollectionUtils.isEmpty(queryCharges)) {
					PosChargeData posChargeData = outParkingFuzzyMatching(configProperties.getParkId(),
							configProperties.getCardNumber());// 车辆出场车牌模糊匹配
					if (posChargeData != null) {
						queryCharges = chargeSerivce.getDebtWithDataV2(posChargeData.getCardNumber(),
								configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
								configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
								configProperties.getMonthUserType(), false,expirationTime);
					}

				}

			} catch (Exception e) {
				configProperties.getDataMap().put("aT", "1");
				configProperties.getDataMap().put("cT", "out");
				configProperties.getDataMap().put("eD", "13");
				configProperties.getDataMap().put("my", "0.0");
				configProperties.getDataMap().put("pT", "9");
				configProperties.getDataMap().put("uT", "0");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			logger.info(configProperties.getCardNumber() + "计费结束----<");
//			double shouldChargeMoney = 0;
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getPosId().contains("收费员:"))) {
				logger.info("判断是否是VIP车辆");
				String subOperatorId = queryCharges.get(0).getPosId().substring(3).trim();
				if(StringUtils.isNotEmpty(subOperatorId)) {
					List<Feeoperator> feeoperator = feeoperatorService.getOperatorByAccount(subOperatorId);
					if(!CollectionUtils.isEmpty(feeoperator)) {
						int money = (int)(Double.valueOf(queryCharges.get(0).getChargeMoney())*100);
						if(feeoperator.get(0).getMoney()-money > 0) {
							feeoperator.get(0).setMoney(feeoperator.get(0).getMoney()-money);
							feeoperatorService.updateByPrimaryKey(feeoperator.get(0));
							configProperties.getDataMap().put("pT", "4");
							logger.info("vip车辆设置aT");
							configProperties.getDataMap().put("aT", "1");
							configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
									queryCharges.get(0).getEntranceDate().getTime()));
							configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
							configProperties.getDataMap().put("uT", "0");
							return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
						}
					}
				}
				
			}
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info("未查询到订单逻辑处理");
				logger.info(configProperties.getCardNumber() + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(configProperties.getPark().getFeeCriterionId());

				if (configProperties.isRealMonthUser && configProperties.getRealMonthUsers().size() == 1) {
					logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV3]车辆入场/出场接口包月用户离场，返回"
							+ Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap()));
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(configProperties.getCardNumber() + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = chargeSerivce
						.getLastRecordWithPark(configProperties.getCardNumber(), 1, configProperties.getParkId());
				if (posChargeDataList.isEmpty()) {
					logger.info("未查到最近一次缴费记录");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", "13");
					logger.info(configProperties.getCardNumber() + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				
				configProperties.getDataMap().put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				handlePushToWechatExitNotice(configProperties, posChargeData);
				Date payDate = posChargeData.getExitDate();

				payDate = getPaidDate(payDate, posChargeData);// 如果进行微信支付宝支付，则需要获取支付时间
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = (new Date().getTime() - payDate.getTime()) / (1000 * 60);

				// 防止逃费
				logger.info(configProperties.getCardNumber() + "开始执行防逃费功能---->");
				if (posChargeData.getPayType() == 9 && diff < 60) {// 1小时内防逃费??到底防逃费防止多久？
					logger.info(configProperties.getCardNumber() + "防逃费，而且时间小于12小时");
					configProperties.getDataMap().put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						logger.info("防逃费应收大于0设置aT");
						configProperties.getDataMap().put("aT", "0");
					} else {
						logger.info("防逃费应收小于0设置aT");
						configProperties.getDataMap().put("aT", "1");
					}
					configProperties.getDataMap().put("eD", getParkingTimeLen(posChargeData.getExitDate().getTime(),
							posChargeData.getEntranceDate().getTime()));
					logger.info(configProperties.getCardNumber() + "结束执行防逃费功能----<防逃费:"
							+ configProperties.getDataMap().toString());
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				if (diff < 15) {
					logger.info(configProperties.getCardNumber() + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", String.valueOf(diff));// 支付时间到现在为止小于15分钟时，直接返回时间即可
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(configProperties.getCardNumber() + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(configProperties.getPark().getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(configProperties.getCardNumber() + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + configProperties.getParkId() + "-" + charge2.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);

					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "前车位数"
							+ configProperties.getPark().getPortLeftCount());
					parkService.updateLeftPortCount(configProperties.getParkId(),
							configProperties.getPark().getPortLeftCount() + 1);
					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "后车位数"
							+ configProperties.getPark().getPortLeftCount() + 1);

					jedisClient.del("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber());
					jedisClient.del("query" + configProperties.getCardNumber());
					logger.info("支付完超15分钟重新入场 " + configProperties.getCardNumber() + " id" + charge2.getId());
					jedisClient.set("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, configProperties.getDataMap());
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
							configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
							configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
							configProperties.getMonthUserType(), false,expirationTime);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(configProperties.getCardNumber() + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(configProperties.getMac());
					if (posChargeData.getParkId() == configProperties.getParkId().intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				configProperties.setShouldChargeMoney(payRet.getChargeMoney());
				
				num = chargeSerivce.update(payRet);
			} else {

				configProperties.getDataMap().put("eD", "14");
				configProperties.getDataMap().put("my", "0.0");
				logger.info(configProperties.getCardNumber() + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}

			logger.info(configProperties.getCardNumber() + "权限-" + configProperties.getMonthUserType());
			hanlderPoschargeInfoExit(configProperties, payRet);

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())
					|| configProperties.getPark().getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, configProperties.getParkId().intValue())) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (configProperties.getParknoticeauthority() != null
					&& configProperties.getParknoticeauthority().getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime", generalFormatTime(payRet.getEntranceDate()));
					argstoali.put("exitTime", generalFormatTime(payRet.getExitDate()));
					logger.info(configProperties.getCardNumber() + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(configProperties.getCardNumber() + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsgWithoutMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			if(diff2 < 0) {
				configProperties.getDataMap().put("eD","13");
			}else {
			configProperties.getDataMap().put("eD",
					String.valueOf((diff2 / (60 * 1000)) > 6360 ? 13 : (diff2 / (60 * 1000))));
			}
			if (!configProperties.isRealMonthUser) {
				configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
			}

			if (configProperties.isMultiCarsOneCarport && configProperties.isRealMonthUser
					&& configProperties.getRealMonthUsers().size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
					configProperties.getDataMap().put("uT", "0");
				}
			}
			configProperties.getDataMap().put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				posChargeMacService.insertSelective(poschargemac);
				logger.info(configProperties.getCardNumber() + "出场成功!" + configProperties.getDataMap().toString());

				parkService.updateLeftPortCount(configProperties.getParkId(),
						configProperties.getPark().getPortLeftCount() + 1);
				logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV3]车辆入场/出场接口");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			} else {
				return Utility.createJsonMsgWithoutMsg(1001, "ok");
			}
		}

	}
	
	@RequestMapping(value = "hefeitouchedV4", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String hefeitouchedV4(@RequestBody Map<String, String> args) throws Exception {
		logger.info("[hefeitouchedV4]入口:"+args);
		String expirationTime = null;
		ConfigProperties configProperties = new ConfigProperties();
		configProperties.setMac(args.get("mac"));
		configProperties.setCardNumber(args.get("cardNumber"));
		logger.info(configProperties.getCardNumber() + "--->进入[hefeitouchedV4]车辆入场/出场接口");
		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		configProperties.setInfos(hardwareService.getInfoByMac(configProperties.getMac()));
		if (!checkInfoValidation(configProperties.getInfos())) {
			return Utility.createJsonMsgWithoutMsg(1002, "get mac info failed!");
		}
		Map<String, Object> info = configProperties.getInfos().get(0);
		configProperties.setChannelFlag((int) info.get("channelFlag"));
		configProperties.setParkId((Integer) info.get("parkID"));
		configProperties.setMonthusers(
				monthUserService.getByCarnumberAndPark(configProperties.getCardNumber(), configProperties.getParkId()));
		configProperties.setPark(parkService.getParkById(configProperties.getParkId()));
		configProperties.setParkcarauthorities(parkCarAuthorityService.getByParkId(configProperties.getParkId()));
		configProperties
				.setCardNumber(getSimilarPlateNumber(configProperties.getCardNumber(), configProperties.getParkId()));
		if (configProperties.getParkcarauthorities().isEmpty()) {
			return null;
		}
		logger.info("第一次设置aT");
		configProperties.getDataMap().put("aT", "1");
		configProperties.setParktoaliparks(parkToAliparkService.getByParkId(configProperties.getParkId()));
		if (checkMultiCarport(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入一位多车停车场");
			configProperties.setIsMultiCarsOneCarport(true);
		}
		if (checkMultiFeeCtriterion(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入多计费标准停车场");
			configProperties.setIsMultiFeeCtriterion(true);
		}
		int direction = 0;
		
		// 入口硬件
		if (configProperties.isEnter()) {
			configProperties.getDataMap().put("lT", String.valueOf(configProperties.getPark().getPortLeftCount()));
			direction = 1;
			//是否是包月用户
			expirationTime = handleMonthUserCarV2(configProperties,direction);
			if(!CollectionUtils.isEmpty(configProperties.getMonthusers()) &&  configProperties.getMonthusers().get(0).getType()== 17){
				configProperties.getDataMap().put("aT", "0");
				configProperties.getDataMap().put("uT", "17");
				configProperties.getDataMap().put("cT", "in");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			// 判断是否有多个车
			configProperties
					.setParknoticeauthority(parkNoticeAuthorityService.getByParkId(configProperties.getParkId()).get(0));
			configProperties.setRealMonthUsers(getMonthUserCars(configProperties.getMonthuserNow()));
			System.out.println(configProperties.isMonthUser);
			handleMonthUserPriority(configProperties);
			setLargeCarStatus(largeCar, configProperties.getCharge());
			configProperties.getCharge().setParkDesc(configProperties.getPark().getName() + "-临停车");

			// 取得入口权限
			getParkEnterAuthority(configProperties);
			hanlderPoschargeInfoEnter(configProperties);
			// 处理一车多位入场
			setPosChargeInfo(configProperties);
			// redis查询
			try {
				String redisStr = jedisClient
						.get("carIn" + configProperties.getParkId() + configProperties.getCardNumber());
				if (redisStr != null) {
					logger.info("redis检测到重复入场" + configProperties.getCardNumber() + redisStr);
					if(CollectionUtils.isNotEmpty(configProperties.getMonthusers())) {
					     configProperties.getMonthusers().get(0).setStatus(configProperties.getMonthusers().get(0).getStatus()+1);
					     monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
					    }
				} else {
					int num = chargeSerivce.insert(configProperties.getCharge());
					// 清场
					handleAutoExit(configProperties);
					if (num == 1) {
						handlePushtoMessageQueue(configProperties);
						logger.info(
								configProperties.getCardNumber() + "入场成功!" + configProperties.getDataMap().toString());
					}
				}
			} catch (Exception e) {
				logger.info(configProperties.getCardNumber() + "redis异常" + e);
			}

			logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV4]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
		} else { // 处理出场逻辑
			direction = 2;
			//是否是包月用户
			expirationTime = handleMonthUserCarV2(configProperties,direction);

			if(!CollectionUtils.isEmpty(configProperties.getMonthusers()) &&  configProperties.getMonthusers().get(0).getType()== 17){
				configProperties.getDataMap().put("aT", "0");
				configProperties.getDataMap().put("uT", "17");
				configProperties.getDataMap().put("cT", "out");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			// 判断是否有多个车
			configProperties
					.setParknoticeauthority(parkNoticeAuthorityService.getByParkId(configProperties.getParkId()).get(0));
			configProperties.setRealMonthUsers(getMonthUserCars(configProperties.getMonthuserNow()));

			configProperties = handleMonthUserPriority(configProperties);
			logger.info(configProperties);
			configProperties.getDataMap().put("cT", "out");
			configProperties.getDataMap().put("pT", "9");
			List<PosChargeData> queryCharges = null;
			logger.info(configProperties.getCardNumber() + "开始出场");
			if (configProperties.getMonthuserNow() != null) {
				configProperties.getRealMonthUsers().add(configProperties.getMonthuserNow());
			}
			// 取得出口权限
			getParkExitAuthority(configProperties);
			try {
				logger.info(configProperties.getCardNumber() + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
						configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
						configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
						configProperties.getMonthUserType(), false,expirationTime);
				if (CollectionUtils.isEmpty(queryCharges)) {
					PosChargeData posChargeData = outParkingFuzzyMatching(configProperties.getParkId(),
							configProperties.getCardNumber());// 车辆出场车牌模糊匹配
					if (posChargeData != null) {
						queryCharges = chargeSerivce.getDebtWithDataV2(posChargeData.getCardNumber(),
								configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
								configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
								configProperties.getMonthUserType(), false,expirationTime);
					}

				}

			} catch (Exception e) {
				configProperties.getDataMap().put("aT", "1");
				configProperties.getDataMap().put("cT", "out");
				configProperties.getDataMap().put("eD", "13");
				configProperties.getDataMap().put("my", "0.0");
				configProperties.getDataMap().put("pT", "9");
				configProperties.getDataMap().put("uT", "0");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			logger.info(configProperties.getCardNumber() + "计费结束----<");
			if((!queryCharges.isEmpty()) && (configProperties.getPark().getDescription().contains("建行无感支付"))) {
				logger.info("建行无感支付");
				DecimalFormat df = new DecimalFormat("#.00");
				DecimalFormat df1 = new DecimalFormat("0.00");
				String amount = "0.00";
				if(queryCharges.get(0).getChargeMoney() > 0) {
					if(queryCharges.get(0).getChargeMoney() > 1) {
						amount = df.format(queryCharges.get(0).getChargeMoney());
					}else {
						amount = df1.format(queryCharges.get(0).getChargeMoney());
					}
					CCBUtil ccb = new CCBUtil();
					boolean bool = ccb.payTest(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()), configProperties.getCardNumber(), amount);
					if(bool) {
						queryCharges.get(0).setPayType(62);
						chargeSerivce.update(queryCharges.get(0));
						configProperties.getDataMap().put("aT", "1");
						configProperties.getDataMap().put("cT", "out");
						configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
								queryCharges.get(0).getEntranceDate().getTime()));
						configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
						configProperties.getDataMap().put("pT", "62");
						configProperties.getDataMap().put("uT", "0");
						jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
						return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
					}
				}
				
			}
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("储值卡"))) {
				if(configProperties.getMonthusers().get(0).getPayment() > queryCharges.get(0).getChargeMoney()) {
					logger.info("公版储值卡逻辑");
					storageCardLogic(configProperties,queryCharges.get(0));
					configProperties.getDataMap().put("aT", "1");
					configProperties.getDataMap().put("cT", "out");
					configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
							queryCharges.get(0).getEntranceDate().getTime()));
					configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
					configProperties.getDataMap().put("pT", "20");
					configProperties.getDataMap().put("uT", "3");
					jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}else {
					configProperties.getMonthusers().get(0).setPayment((float)(configProperties.getMonthusers().get(0).getPayment() - queryCharges.get(0).getChargeMoney()));
					monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
					configProperties.getDataMap().put("aT", "0");
					configProperties.getDataMap().put("cT", "out");
					configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
							queryCharges.get(0).getEntranceDate().getTime()));
					configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
					configProperties.getDataMap().put("pT", "30");
					configProperties.getDataMap().put("uT", "3");
					jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
			}
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getPosId().contains("收费员:"))) {
				logger.info("判断是否是VIP车辆");
				String subOperatorId = queryCharges.get(0).getPosId().substring(3).trim();
				if(StringUtils.isNotEmpty(subOperatorId)) {
					List<Feeoperator> feeoperator = feeoperatorService.getOperatorByAccount(subOperatorId);
					if(!CollectionUtils.isEmpty(feeoperator)) {
						int money = (int)(Double.valueOf(queryCharges.get(0).getChargeMoney())*100);
						if(feeoperator.get(0).getMoney()-money > 0) {
							feeoperator.get(0).setMoney(feeoperator.get(0).getMoney()-money);
							feeoperatorService.updateByPrimaryKey(feeoperator.get(0));
							configProperties.getDataMap().put("pT", "4");
							logger.info("vip车辆设置aT");
							configProperties.getDataMap().put("aT", "1");
							configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
									queryCharges.get(0).getEntranceDate().getTime()));
							configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
							configProperties.getDataMap().put("uT", "0");
							jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
							return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
						}
					}
				}
				
			}
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("临停车"))) {
				configProperties.getDataMap().put("uT", "0");
			}
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info("未查询到订单逻辑处理");
				logger.info(configProperties.getCardNumber() + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(configProperties.getPark().getFeeCriterionId());

				if (configProperties.isRealMonthUser && configProperties.getRealMonthUsers().size() == 1) {
					logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV4]车辆入场/出场接口包月用户离场，返回"
							+ Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap()));
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(configProperties.getCardNumber() + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = chargeSerivce
						.getLastRecordWithPark(configProperties.getCardNumber(), 1, configProperties.getParkId());
				if (posChargeDataList.isEmpty()) {
					logger.info("未查到最近一次缴费记录");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", "13");
					logger.info(configProperties.getCardNumber() + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				
				configProperties.getDataMap().put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				handlePushToWechatExitNotice(configProperties, posChargeData);
				Date payDate = posChargeData.getExitDate();

				payDate = getPaidDate(payDate, posChargeData);// 如果进行微信支付宝支付，则需要获取支付时间
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = (new Date().getTime() - payDate.getTime()) / (1000 * 60);

				// 防止逃费
				logger.info(configProperties.getCardNumber() + "开始执行防逃费功能---->");
				  String[] descriptions = configProperties.getPark().getDescription().replace("；",",").split(";");
			        String dealyStr = "";
			        for (String tmpStr : descriptions) {
			            if (tmpStr.contains("防逃费")) {
			                dealyStr = tmpStr.split("-")[1];
			            }
			        }
			        Long maxDelay=(long) 12;
			        if (StringUtils.isNotBlank(dealyStr)) {
						maxDelay=(long) Integer.parseInt(dealyStr);
					}
			        Long second = (long)1000 * (long)60 * (long)60*maxDelay;
				if ((posChargeData.getPayType() == 9) && (diff < second)) {
					configProperties.getDataMap().put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						configProperties.getDataMap().put("aT", "0");
					} else {
						configProperties.getDataMap().put("aT", "1");
					}
					configProperties.getDataMap().put("eD", getParkingTimeLen(posChargeData.getExitDate().getTime(),
							posChargeData.getEntranceDate().getTime()));
					logger.info(configProperties.getCardNumber() + "结束执行防逃费功能----<防逃费:"
							+ configProperties.getDataMap().toString());
					jedisClient.set("out"+configProperties.getMac(), String.valueOf(posChargeData.getId()));
					if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId())
							|| configProperties.getPark().getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(configProperties.getParkId()), configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
								configProperties.getMonthUserType());
					}
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				if (diff < 15) {
					logger.info(configProperties.getCardNumber() + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", String.valueOf(diff));// 支付时间到现在为止小于15分钟时，直接返回时间即可
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(configProperties.getCardNumber() + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(configProperties.getPark().getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(configProperties.getCardNumber() + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + configProperties.getParkId() + "-" + charge2.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);

					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "前车位数"
							+ configProperties.getPark().getPortLeftCount());
					parkService.updateLeftPortCount(configProperties.getParkId(),
							configProperties.getPark().getPortLeftCount() + 1);
					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "后车位数"
							+ configProperties.getPark().getPortLeftCount() + 1);

					jedisClient.del("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber());
					jedisClient.del("query" + configProperties.getCardNumber());
					logger.info("支付完超15分钟重新入场 " + configProperties.getCardNumber() + " id" + charge2.getId());
					jedisClient.set("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, configProperties.getDataMap());
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
							configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
							configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
							configProperties.getMonthUserType(), false,expirationTime);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(configProperties.getCardNumber() + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(configProperties.getMac());
					if (posChargeData.getParkId() == configProperties.getParkId().intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				configProperties.setShouldChargeMoney(payRet.getChargeMoney());
				num = chargeSerivce.update(payRet);
			} else {

				configProperties.getDataMap().put("eD", "14");
				configProperties.getDataMap().put("my", "0.0");
				logger.info(configProperties.getCardNumber() + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}

			logger.info(configProperties.getCardNumber() + "权限-" + configProperties.getMonthUserType());
			hanlderPoschargeInfoExit(configProperties, payRet);

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())
					|| configProperties.getPark().getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, configProperties.getParkId().intValue())) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (configProperties.getParknoticeauthority() != null
					&& configProperties.getParknoticeauthority().getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime", generalFormatTime(payRet.getEntranceDate()));
					argstoali.put("exitTime", generalFormatTime(payRet.getExitDate()));
					logger.info(configProperties.getCardNumber() + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(configProperties.getCardNumber() + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsgWithoutMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			if(diff2 < 0) {
				configProperties.getDataMap().put("eD","13");
			}else {
			configProperties.getDataMap().put("eD",
					String.valueOf((diff2 / (60 * 1000)) > 6360 ? 13 : (diff2 / (60 * 1000))));
			}
			if (!configProperties.isRealMonthUser) {
				configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
			}

			if (configProperties.isMultiCarsOneCarport && configProperties.isRealMonthUser
					&& configProperties.getRealMonthUsers().size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
					configProperties.getDataMap().put("uT", "0");
				}
			}
			configProperties.getDataMap().put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				posChargeMacService.insertSelective(poschargemac);
				if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("包月过期")) && (queryCharges.get(0).getChargeMoney() == 0)) {
					configProperties.getDataMap().put("aT", "1");
				}
				if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("临停车"))) {
					configProperties.getDataMap().put("uT", "0");
					configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
				}
				logger.info(configProperties.getCardNumber() + "出场成功!" + configProperties.getDataMap().toString());

				parkService.updateLeftPortCount(configProperties.getParkId(),
						configProperties.getPark().getPortLeftCount() + 1);
				logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV4]车辆入场/出场接口");
				jedisClient.set("out"+configProperties.getMac(), String.valueOf(payRet.getId()));
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			} else {
				return Utility.createJsonMsgWithoutMsg(1001, "ok");
			}
		}

	}
	private void parkRecord(int poschargeId) {
		ParkRecordData parkRecordData = chargeSerivce.getByIdV1(poschargeId);
		parkRecordData.setPaidMoney(parkRecordData.getChargeMoney());
		parkRecordData.setUnPaidMoney(0);
		logger.info(parkRecordData);
		parkRecordDataService.insert(parkRecordData);
		chargeSerivce.deleteById(parkRecordData.getId());
	}
	@RequestMapping(value = "hefeitouchedV5", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String hefeitouchedV5(@RequestBody Map<String, String> args) throws Exception {
		logger.info("[hefeitouchedV5]入口:"+args);
		String expirationTime = null;
		ConfigProperties configProperties = new ConfigProperties();
		configProperties.setMac(args.get("mac"));
		configProperties.setCardNumber(args.get("cardNumber"));
		logger.info(configProperties.getCardNumber() + "--->进入[hefeitouchedV5]车辆入场/出场接口");
		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		configProperties.setInfos(hardwareService.getInfoByMac(configProperties.getMac()));
		if (!checkInfoValidation(configProperties.getInfos())) {
			return Utility.createJsonMsgWithoutMsg(1002, "get mac info failed!");
		}
		Map<String, Object> info = configProperties.getInfos().get(0);
		configProperties.setChannelFlag((int) info.get("channelFlag"));
		configProperties.setParkId((Integer) info.get("parkID"));
		configProperties.setMonthusers(
				monthUserService.getByCarnumberAndPark(configProperties.getCardNumber(), configProperties.getParkId()));
		configProperties.setPark(parkService.getParkById(configProperties.getParkId()));
		configProperties.setParkcarauthorities(parkCarAuthorityService.getByParkId(configProperties.getParkId()));
		configProperties
				.setCardNumber(getSimilarPlateNumber(configProperties.getCardNumber(), configProperties.getParkId()));
		if (configProperties.getParkcarauthorities().isEmpty()) {
			return null;
		}
		logger.info("第一次设置aT");
		configProperties.getDataMap().put("aT", "1");
		configProperties.setParktoaliparks(parkToAliparkService.getByParkId(configProperties.getParkId()));
		if (checkMultiCarport(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入一位多车停车场");
			configProperties.setIsMultiCarsOneCarport(true);
		}
		if (checkMultiFeeCtriterion(configProperties.getPark())) {
			logger.info(configProperties.getCardNumber() + "进入多计费标准停车场");
			configProperties.setIsMultiFeeCtriterion(true);
		}
		int direction = 0;
		
		// 入口硬件
		if (configProperties.isEnter()) {
			configProperties.getDataMap().put("lT", String.valueOf(configProperties.getPark().getPortLeftCount()));
			direction = 1;
			//是否是包月用户
			expirationTime = handleMonthUserCarV2(configProperties,direction);
			if(!CollectionUtils.isEmpty(configProperties.getMonthusers()) &&  configProperties.getMonthusers().get(0).getType()== 17){
				configProperties.getDataMap().put("aT", "0");
				configProperties.getDataMap().put("uT", "17");
				configProperties.getDataMap().put("cT", "in");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			// 判断是否有多个车
			configProperties
					.setParknoticeauthority(parkNoticeAuthorityService.getByParkId(configProperties.getParkId()).get(0));
			configProperties.setRealMonthUsers(getMonthUserCars(configProperties.getMonthuserNow()));
			System.out.println(configProperties.isMonthUser);
			handleMonthUserPriority(configProperties);
			setLargeCarStatus(largeCar, configProperties.getCharge());
			configProperties.getCharge().setParkDesc(configProperties.getPark().getName() + "-临停车");

			// 取得入口权限
			getParkEnterAuthority(configProperties);
			hanlderPoschargeInfoEnter(configProperties);
			// 处理一车多位入场
			setPosChargeInfo(configProperties);
			// redis查询
			try {
				String redisStr = jedisClient
						.get("carIn" + configProperties.getParkId() + configProperties.getCardNumber());
				if (redisStr != null) {
					logger.info("redis检测到重复入场" + configProperties.getCardNumber() + redisStr);
					if(CollectionUtils.isNotEmpty(configProperties.getMonthusers())) {
					     configProperties.getMonthusers().get(0).setStatus(configProperties.getMonthusers().get(0).getStatus()+1);
					     monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
					    }
				} else {
					int num = chargeSerivce.insert(configProperties.getCharge());
					// 清场
					handleAutoExit(configProperties);
					if (num == 1) {
						handlePushtoMessageQueue(configProperties);
						logger.info(
								configProperties.getCardNumber() + "入场成功!" + configProperties.getDataMap().toString());
					}
				}
			} catch (Exception e) {
				logger.info(configProperties.getCardNumber() + "redis异常" + e);
			}

			logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV5]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
		} else { // 处理出场逻辑
			direction = 2;
			//是否是包月用户
			expirationTime = handleMonthUserCarV2(configProperties,direction);

			if(!CollectionUtils.isEmpty(configProperties.getMonthusers()) &&  configProperties.getMonthusers().get(0).getType()== 17){
				configProperties.getDataMap().put("aT", "0");
				configProperties.getDataMap().put("uT", "17");
				configProperties.getDataMap().put("cT", "out");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			// 判断是否有多个车
			configProperties
					.setParknoticeauthority(parkNoticeAuthorityService.getByParkId(configProperties.getParkId()).get(0));
			configProperties.setRealMonthUsers(getMonthUserCars(configProperties.getMonthuserNow()));

			configProperties = handleMonthUserPriority(configProperties);
			logger.info(configProperties);
			configProperties.getDataMap().put("cT", "out");
			configProperties.getDataMap().put("pT", "9");
			List<PosChargeData> queryCharges = null;
			logger.info(configProperties.getCardNumber() + "开始出场");
			if (configProperties.getMonthuserNow() != null) {
				configProperties.getRealMonthUsers().add(configProperties.getMonthuserNow());
			}
			// 取得出口权限
			getParkExitAuthority(configProperties);
			try {
				logger.info(configProperties.getCardNumber() + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
						configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
						configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
						configProperties.getMonthUserType(), false,expirationTime);
				if (CollectionUtils.isEmpty(queryCharges)) {
					PosChargeData posChargeData = outParkingFuzzyMatching(configProperties.getParkId(),
							configProperties.getCardNumber());// 车辆出场车牌模糊匹配
					if (posChargeData != null) {
						queryCharges = chargeSerivce.getDebtWithDataV2(posChargeData.getCardNumber(),
								configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
								configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
								configProperties.getMonthUserType(), false,expirationTime);
					}

				}

			} catch (Exception e) {
				configProperties.getDataMap().put("aT", "1");
				configProperties.getDataMap().put("cT", "out");
				configProperties.getDataMap().put("eD", "13");
				configProperties.getDataMap().put("my", "0.0");
				configProperties.getDataMap().put("pT", "9");
				configProperties.getDataMap().put("uT", "0");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}
			logger.info(configProperties.getCardNumber() + "计费结束----<");
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("储值卡"))) {
				if(configProperties.getMonthusers().get(0).getPayment() > queryCharges.get(0).getChargeMoney()) {
					logger.info("公版储值卡逻辑");
					storageCardLogic(configProperties,queryCharges.get(0));
					configProperties.getDataMap().put("aT", "1");
					configProperties.getDataMap().put("cT", "out");
					configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
							queryCharges.get(0).getEntranceDate().getTime()));
					configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
					configProperties.getDataMap().put("pT", "20");
					configProperties.getDataMap().put("uT", "3");
					jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
					if(configProperties.getPark().getDescription().contains("启用新平台")) {
						logger.info("启用新平台");
						this.parkRecord(queryCharges.get(0).getId());
					}
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}else {
					configProperties.getMonthusers().get(0).setPayment((float)(configProperties.getMonthusers().get(0).getPayment() - queryCharges.get(0).getChargeMoney()));
					monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
					configProperties.getDataMap().put("aT", "0");
					configProperties.getDataMap().put("cT", "out");
					configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
							queryCharges.get(0).getEntranceDate().getTime()));
					configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
					configProperties.getDataMap().put("pT", "30");
					configProperties.getDataMap().put("uT", "3");
					jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
					if(configProperties.getPark().getDescription().contains("启用新平台")) {
						logger.info("启用新平台");
						this.parkRecord(queryCharges.get(0).getId());
					}
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
			}
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getPosId().contains("收费员:"))) {
				logger.info("判断是否是VIP车辆");
				String subOperatorId = queryCharges.get(0).getPosId().substring(3).trim();
				if(StringUtils.isNotEmpty(subOperatorId)) {
					List<Feeoperator> feeoperator = feeoperatorService.getOperatorByAccount(subOperatorId);
					if(!CollectionUtils.isEmpty(feeoperator)) {
						int money = (int)(Double.valueOf(queryCharges.get(0).getChargeMoney())*100);
						if(feeoperator.get(0).getMoney()-money > 0) {
							feeoperator.get(0).setMoney(feeoperator.get(0).getMoney()-money);
							feeoperatorService.updateByPrimaryKey(feeoperator.get(0));
							configProperties.getDataMap().put("pT", "4");
							logger.info("vip车辆设置aT");
							configProperties.getDataMap().put("aT", "1");
							configProperties.getDataMap().put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
									queryCharges.get(0).getEntranceDate().getTime()));
							configProperties.getDataMap().put("my", String.valueOf(queryCharges.get(0).getChargeMoney()));
							configProperties.getDataMap().put("uT", "0");
							jedisClient.set("out"+configProperties.getMac(), String.valueOf(queryCharges.get(0).getId()));
							if(configProperties.getPark().getDescription().contains("启用新平台")) {
								logger.info("启用新平台");
								this.parkRecord(queryCharges.get(0).getId());
							}
							return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
						}
					}
				}
				
			}
			if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("临停车"))) {
				configProperties.getDataMap().put("uT", "0");
			}
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info("未查询到订单逻辑处理");
				logger.info(configProperties.getCardNumber() + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(configProperties.getPark().getFeeCriterionId());

				if (configProperties.isRealMonthUser && configProperties.getRealMonthUsers().size() == 1) {
					logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV5]车辆入场/出场接口包月用户离场，返回"
							+ Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap()));
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(configProperties.getCardNumber() + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = new ArrayList<PosChargeData>();
				if(configProperties.getPark().getDescription().contains("启用新平台")) {
					posChargeDataList= parkRecordDataService
							.getLastRecordWithPark(configProperties.getCardNumber(), 1, configProperties.getParkId());
				}else {
					posChargeDataList= chargeSerivce
							.getLastRecordWithPark(configProperties.getCardNumber(), 1, configProperties.getParkId());
				}
				 
				if (posChargeDataList.isEmpty()) {
					logger.info("未查到最近一次缴费记录");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", "13");
					logger.info(configProperties.getCardNumber() + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(configProperties.getCardNumber() + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				
				configProperties.getDataMap().put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				handlePushToWechatExitNotice(configProperties, posChargeData);
				Date payDate = posChargeData.getExitDate();

				payDate = getPaidDate(payDate, posChargeData);// 如果进行微信支付宝支付，则需要获取支付时间
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = (new Date().getTime() - payDate.getTime()) / (1000 * 60);

				// 防止逃费
				logger.info(configProperties.getCardNumber() + "开始执行防逃费功能---->");
				if (posChargeData.getPayType() == 9 && diff < 60) {// 1小时内防逃费??到底防逃费防止多久？
					logger.info(configProperties.getCardNumber() + "防逃费，而且时间小于12小时");
					configProperties.getDataMap().put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						logger.info("防逃费应收大于0设置aT");
						configProperties.getDataMap().put("aT", "0");
					} else {
						logger.info("防逃费应收小于0设置aT");
						configProperties.getDataMap().put("aT", "1");
					}
					configProperties.getDataMap().put("eD", getParkingTimeLen(posChargeData.getExitDate().getTime(),
							posChargeData.getEntranceDate().getTime()));
					logger.info(configProperties.getCardNumber() + "结束执行防逃费功能----<防逃费:"
							+ configProperties.getDataMap().toString());
					jedisClient.set("out"+configProperties.getMac(), String.valueOf(posChargeData.getId()));
					if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId())
							|| configProperties.getPark().getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(configProperties.getParkId()), configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
								configProperties.getMonthUserType());
					}
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				if (diff < 15) {
					logger.info(configProperties.getCardNumber() + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					configProperties.getDataMap().put("my", "0.0");
					configProperties.getDataMap().put("eD", String.valueOf(diff));// 支付时间到现在为止小于15分钟时，直接返回时间即可
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(configProperties.getCardNumber() + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(configProperties.getPark().getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(configProperties.getCardNumber() + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + configProperties.getParkId() + "-" + charge2.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);

					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "前车位数"
							+ configProperties.getPark().getPortLeftCount());
					parkService.updateLeftPortCount(configProperties.getParkId(),
							configProperties.getPark().getPortLeftCount() + 1);
					logger.info(configProperties.getCardNumber() + "离开" + configProperties.getPark().getName() + "后车位数"
							+ configProperties.getPark().getPortLeftCount() + 1);

					jedisClient.del("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber());
					jedisClient.del("query" + configProperties.getCardNumber());
					logger.info("支付完超15分钟重新入场 " + configProperties.getCardNumber() + " id" + charge2.getId());
					jedisClient.set("P-" + configProperties.getParkId() + "-" + configProperties.getCardNumber(),
							String.valueOf(charge2.getId()), Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, configProperties.getDataMap());
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithDataV2(configProperties.getCardNumber(),
							configProperties.getParktoaliparks(), configProperties.getRealMonthUsers(),
							configProperties.getPark(), configProperties.getIsMultiCarsOneCarport(),
							configProperties.getMonthUserType(), false,expirationTime);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(configProperties.getCardNumber() + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(configProperties.getMac());
					if (posChargeData.getParkId() == configProperties.getParkId().intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				configProperties.setShouldChargeMoney(payRet.getChargeMoney());
				num = chargeSerivce.update(payRet);
			} else {

				configProperties.getDataMap().put("eD", "14");
				configProperties.getDataMap().put("my", "0.0");
				logger.info(configProperties.getCardNumber() + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			}

			logger.info(configProperties.getCardNumber() + "权限-" + configProperties.getMonthUserType());
			hanlderPoschargeInfoExit(configProperties, payRet);

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, configProperties.getParkId().intValue())
					|| configProperties.getPark().getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(configProperties.getParkId()),
						configProperties.getMac(), configProperties.getPark().getPortLeftCount(),
						configProperties.monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, configProperties.getParkId().intValue())) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (configProperties.getParknoticeauthority() != null
					&& configProperties.getParknoticeauthority().getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime", generalFormatTime(payRet.getEntranceDate()));
					argstoali.put("exitTime", generalFormatTime(payRet.getExitDate()));
					logger.info(configProperties.getCardNumber() + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(configProperties.getCardNumber() + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsgWithoutMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			if(diff2 < 0) {
				configProperties.getDataMap().put("eD","13");
			}else {
			configProperties.getDataMap().put("eD",
					String.valueOf((diff2 / (60 * 1000)) > 6360 ? 13 : (diff2 / (60 * 1000))));
			}
			if (!configProperties.isRealMonthUser) {
				configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
			}

			if (configProperties.isMultiCarsOneCarport && configProperties.isRealMonthUser
					&& configProperties.getRealMonthUsers().size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
					configProperties.getDataMap().put("uT", "0");
				}
			}
			configProperties.getDataMap().put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				posChargeMacService.insertSelective(poschargemac);
				if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("包月过期")) && (queryCharges.get(0).getChangeMoney() == 0)) {
					configProperties.getDataMap().put("aT", "1");
				}
				if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("临停车"))) {
					configProperties.getDataMap().put("uT", "0");
					configProperties.getDataMap().put("my", String.valueOf(payRet.getChargeMoney()));
				}
				logger.info(configProperties.getCardNumber() + "出场成功!" + configProperties.getDataMap().toString());

				parkService.updateLeftPortCount(configProperties.getParkId(),
						configProperties.getPark().getPortLeftCount() + 1);
				logger.info(configProperties.getCardNumber() + "<---离开[hefeitouchedV5]车辆入场/出场接口");
				jedisClient.set("out"+configProperties.getMac(), String.valueOf(payRet.getId()));
				if(configProperties.getPark().getDescription().contains("启用新平台")) {
					logger.info("启用新平台");
					this.parkRecord(queryCharges.get(0).getId());
				}
				return Utility.createJsonMsgWithoutMsg(1001, configProperties.getDataMap());
			} else {
				return Utility.createJsonMsgWithoutMsg(1001, "ok");
			}
		}

	}
	private void storageCardLogic(ConfigProperties configProperties, PosChargeData posChargeData) {
		configProperties.getMonthusers().get(0).setPayment((float)(configProperties.getMonthusers().get(0).getPayment() - posChargeData.getChargeMoney()));
		monthUserService.updateByPrimaryKey(configProperties.getMonthusers().get(0));
		Monthuserconsumerrecord monthuserconsumerrecord = new Monthuserconsumerrecord();
		monthuserconsumerrecord.setBeforerecharge((double) configProperties.getMonthusers().get(0).getPayment());
		monthuserconsumerrecord.setParkid(posChargeData.getParkId());
		monthuserconsumerrecord.setParkdesc(posChargeData.getParkDesc());
		monthuserconsumerrecord.setOperationtype(0);
		monthuserconsumerrecord.setPoschargedataid(posChargeData.getId());
		monthuserconsumerrecord.setPlatenumber(posChargeData.getCardNumber());
		monthuserconsumerrecord.setName(configProperties.getMonthusers().get(0).getOwner());
		monthuserconsumerrecord.setShouldcharge(posChargeData.getChargeMoney());

		configProperties.getMonthusers().get(0).setPayment((float) (configProperties.getMonthusers().get(0).getPayment() - posChargeData.getChargeMoney()));
		monthuserconsumerrecord.setBalance((double) configProperties.getMonthusers().get(0).getPayment());
		monthuserConsumerService.insertSelective(monthuserconsumerrecord);
		
	}

	public PosChargeData outParkingFuzzyMatching(int parkId, String cardNumber) throws ParseException {
		logger.info("模糊匹配查询出场参数：" + parkId + "\t" +cardNumber);
		PosChargeData posChargeData = null;
		List<PosChargeData> data = new ArrayList<>();
		List<String> cardNumbers = new ArrayList<>();
		Map<String, PosChargeData> maps = new HashMap<>();
		char[] bytes = cardNumber.toCharArray();
			data = chargeSerivce.getAllByDayRangeV1(parkId, null, null);
			for (PosChargeData pos : data) {
				cardNumbers.add(pos.getCardNumber());
				maps.put(pos.getCardNumber(), pos);
			}
			for (String cn : cardNumbers) {
				char[] cnchar = cn.toCharArray();
				int count = 0;
				int length = (bytes.length < cnchar.length) ? bytes.length :cnchar.length;
				for (int i = 0; i < length; i++) {
					if (bytes[i] ==cnchar[i]) {
						count = count + 1;
					}
				}
				if (count == bytes.length - 1) {
					posChargeData = maps.get(cn);
					logger.info(posChargeData);
					break;
				}
			}
		logger.info(
				"模糊匹配出场车辆记录：" + posChargeData + "\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		return posChargeData;
	}
	@RequestMapping(value = "/updatePosId", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public @ResponseBody String updatePosId(@RequestBody Map<String, Object> args) throws ParseException {
		logger.info("[updatePosId]参数:"+args);
		int parkId = (int)args.get("parkId");
		String plateNumber = (String)args.get("plateNumber");
		String posId = (String)args.get("posId");
		Map<String,Object> resultMap = new HashMap<String, Object>();
		try {
		List<PosChargeData> posChargeDatas = chargeSerivce.getByCardNumberAndParkV1(plateNumber, parkId);
		if(CollectionUtils.isEmpty(posChargeDatas)) {
			PosChargeData posChargeData = outParkingFuzzyMatching(parkId,plateNumber);
			if(posChargeData != null) {
				posChargeData.setPosId(posId);
				int count = chargeSerivce.update(posChargeData);
				if(count > 0) {
					resultMap.put("status",1001);
					resultMap.put("message","修改成功");
				}else {
					resultMap.put("status",1002);
					resultMap.put("message","修改失败");
				}
			}else {
				resultMap.put("status",1002);
				resultMap.put("message","查无此车");
			}
		}else {
			posChargeDatas.get(0).setPosId(posId);
			int count = chargeSerivce.update(posChargeDatas.get(0));
			if(count > 0) {
				resultMap.put("status",1001);
				resultMap.put("message","修改成功");
			}else {
				resultMap.put("status",1002);
				resultMap.put("message","修改失败");
			}
		}
		}catch (Exception e) {
			resultMap.put("status",1002);
			resultMap.put("message","查无此车");
		}
		logger.info("[updatePosId]返回值:"+resultMap);
		return Utility.gson.toJson(resultMap);
	}
}
